package com.android.videotest.video.data;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

public class VideoInfoHelper {
    /**
     * 获取手机中所有视频的信息
     * @return
     */
    public static ArrayList<MediaBean> getAllVideoInfos(Context context) {
//        HashMap<Folder, List<MediaBean>> data = new HashMap<>();
        ArrayList<MediaBean> mediaBeans = new ArrayList<>();
        Uri imageUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] proj = {MediaStore.Video.Thumbnails._ID
                , MediaStore.Video.Thumbnails.DATA
                , MediaStore.Video.Media.DURATION
                , MediaStore.Video.Media.SIZE
                , MediaStore.Video.Media.DISPLAY_NAME
                , MediaStore.Video.Media.DATE_MODIFIED};
        Cursor query = context.getContentResolver().query(imageUri, proj,
                MediaStore.Video.Media.MIME_TYPE + "=?", new String[]{"video/mp4"},
                MediaStore.Video.Media.DATE_MODIFIED + " desc");
        if (query != null) {
            while (query.moveToNext()) {
                // 获取视频的路径
                int videoId = query.getInt(query.getColumnIndex(MediaStore.Video.Media._ID));
                String path = query.getString(query.getColumnIndex(MediaStore.Video.Media.DATA));
                int duration = query.getInt(query.getColumnIndex(MediaStore.Video.Media.DURATION));
                long size = query.getLong(query.getColumnIndex(MediaStore.Video.Media.SIZE)) / 1024; //单位kb
                if (size < 0) {
                    //某些设备获取size<0，直接计算
                    Log.e("dml", "this video size < 0 " + path);
                    size = new File(path).length() / 1024;
                }
                String displayName = query.getString(query.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
                long modifyTime = query.getLong(query.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED));//暂未用到

                //提前生成缩略图，再获取：http://stackoverflow.com/questions/27903264/how-to-get-the-video-thumbnail-path-and-not-the-bitmap
                MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), videoId, MediaStore.Video.Thumbnails.MICRO_KIND, null);
                String[] projection = {MediaStore.Video.Thumbnails._ID, MediaStore.Video.Thumbnails.DATA};
                Cursor cursor = context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI
                        , projection
                        , MediaStore.Video.Thumbnails.VIDEO_ID + "=?"
                        , new String[]{videoId + ""}
                        , null);
                String thumbPath = "";
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        thumbPath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                    }
                    cursor.close();
                }
                // 获取该视频的父路径名
                String dirPath = Objects.requireNonNull(new File(path).getParentFile()).getAbsolutePath();
                //存储对应关系
                mediaBeans.add(new MediaBean(MediaBean.Type.VIDEO, path, thumbPath, duration, size, displayName, dirPath));

//                if (folders.containsKey(dirPath)) {
//                    List<MediaBean> data = folders.get(dirPath);
//                    if (data == null) {
//                        data = new ArrayList<>();
//                    }
//                    data.add(new MediaBean(MediaBean.Type.VIDEO, path, thumbPath, duration, size, displayName));
//                } else {
//                    List<MediaBean> data = new ArrayList<>();
//                    data.add(new MediaBean(MediaBean.Type.VIDEO, path, thumbPath, duration, size, displayName));
//                    folders.put(dirPath, data);
//                }
            }
            query.close();
            return  mediaBeans;
        }
        return mediaBeans;
    }
}
